package com.all.widget.tree;

import java.util.ArrayList;
import java.util.List;


public class Node {
	//节点ID
	private int id;
	//节点名称
	private String name;
	/**
	 * 根节点pId为0
	 */
	private int pId = 0;
	/**
	 * 当前的级别
	 */
	@SuppressWarnings("unused")
	private int level;

	/**
	 * 是否展开
	 */
	private boolean isExpand = false;

	/**
	 * 图标
	 */
	private int icon;

	/**
	 * 下一级的子Node
	 */
	private List<Node> children = new ArrayList<Node>();

	/**
	 * 父Node
	 */
	private Node parent=null;
	
	/**
	 * 是否是子节点，动态添加树时要要用，不然的话可以用children来判断
	 */
	private boolean isLeaf=false ;


	public void setLeaf(boolean isLeaf) {
		this.isLeaf = isLeaf;
	}

	public Node()
	{
	}

	public Node(int id, int pId, String name)
	{
		super();
		this.id = id;
		this.pId = pId;
		this.name = name;
	}

	public int getIcon()
	{
		return icon;
	}

	public void setIcon(int icon)
	{
		this.icon = icon;
	}

	public int getId()
	{
		return id;
	}

	public void setId(int id)
	{
		this.id = id;
	}

	public int getpId()
	{
		return pId;
	}

	public void setpId(int pId)
	{
		this.pId = pId;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public void setLevel(int level)
	{
		this.level = level;
	}

	public boolean isExpand()
	{
		return isExpand;
	}

	public List<Node> getChildren()
	{
		return children;
	}

	public void setChildren(List<Node> children)
	{
		this.children = children;
	}

	public Node getParent()
	{
		return parent;
	}

	public void setParent(Node parent)
	{
		this.parent = parent;
	}

	/**
	 * 是否为跟节点
	 * 
	 * @return
	 */
	public boolean isRoot()
	{
		return parent == null;
	}

	/**
	 * 判断父节点是否展开
	 * 
	 * @return
	 */
	public boolean isParentExpand()
	{
		if (parent == null)
			return false;
		return parent.isExpand();
	}

	/**
	 * 是否是叶子界点
	 * 
	 * @return
	 */
	public boolean isLeaf()
	{
		if(children.size()!=0||!isLeaf)
			isLeaf= false;
		return isLeaf;
	}

	/**
	 * 非动态的时候可以根据ChildSize来判断是否是子节点
	 * 
	 * @return
	 */
	public int getChildSize()
	{
		return children.size();
	}
	
	/**
	 * 获取level
	 */
	public int getLevel()
	{
		return parent == null ? 0 : parent.getLevel() + 1;
	}

	/**
	 * 设置展开
	 * 
	 * @param isExpand
	 */
	public void setExpand(boolean isExpand)
	{
		this.isExpand = isExpand;
		if (!isExpand)
		{

			for (Node node : children)
			{
				node.setExpand(isExpand);
			}
		}
	}

	/**
	 * 动态的添加子节点
	 * @param childNodes
	 */
	public void addChild(List<Node> childNodes) {
		// TODO Auto-generated method stub
		children.addAll(childNodes);
	}
}
